//
//  LifeInfoViewController.m
//  心水宝典
//
//  Created by zr on 2018/8/31.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "LifeInfoViewController.h"

@interface LifeInfoViewController ()<UIWebViewDelegate>

@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, weak)UIImageView * contentImageView;
@property (nonatomic, weak)UIWebView * webView;


@end

@implementation LifeInfoViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WHITE_COLOR;
    
    [self setupNav];
    [self setupUI];
}


- (void)setupNav{
    
    self.title = @"命理详情";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    //    backButton.frame = CGRectMake(0, 0, 20 * SCREENH6, 20 * SCREENH6);
    backButton.size = CGSizeMake(20 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}


- (void)setupUI{
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.textColor = BLACK_COLOR;
    titleLabel.font = [UIFont systemFontOfSize:20 * SCREENH6];
    titleLabel.text = self.model.title;
    
    self.titleLabel = titleLabel;
    
    [self.view addSubview:self.titleLabel];
    
    UIImageView * contentImageView = [UIImageView new];
    
//    contentImageView.backgroundColor = [UIColor lightGrayColor];
    NSURL * url = [NSURL URLWithString:self.model.img];
    [contentImageView sd_setImageWithURL:url];
    
    self.contentImageView = contentImageView;
    
    [self.view addSubview:self.contentImageView];
    
    UIWebView *webView = [[UIWebView alloc] init];
    webView.scalesPageToFit = YES;
    webView.delegate = self;
    webView.backgroundColor = WHITE_COLOR;
    NSURL * urlPath = [NSURL fileURLWithPath:[NSBundle mainBundle].bundlePath];
    [webView loadHTMLString:self.model.content baseURL:urlPath];

    
    self.webView = webView;
    
    [self.view addSubview:self.webView];
    
    
}

- (void)viewDidLayoutSubviews{
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(10 * SCREENH6);
    }];
    
    [self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10 * SCREENH6);
        make.height.mas_equalTo(200 * SCREENH6);
    }];
    
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.contentImageView);
        make.top.mas_equalTo(self.contentImageView.mas_bottom).offset(10 * SCREENH6);
        make.bottom.mas_equalTo(self.view).offset(-5 * SCREENH6);
    }];
}


- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '250%'"];
}

- (void)backToView {
    
    [self.navigationController popViewControllerAnimated:YES];
}



@end
